package org.jeecgframework.web.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;

import org.activiti.engine.task.Task;
import org.jeecgframework.core.util.ApplicationContextUtil;
import org.jeecgframework.core.util.ResourceUtil;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.web.service.ZyCommonService;
import org.jeecgframework.web.service.ZyWorkFlowService;
import org.jeecgframework.web.system.pojo.base.TSUser;
import org.jeecgframework.web.system.pojo.base.ZyInspectionPlanEntity;
import org.jeecgframework.web.system.pojo.base.ZyPlanRecordEntity;
import org.jeecgframework.web.system.service.SystemService;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

/**
 * @author xiul
 * 2016-4-1 下午6:30:43
 * Description 
 */
public class ZyProcessThreadSetpTwo implements Callable<Map<String, Object>> {
	
	private static SystemService systemService = ApplicationContextUtil.getBean("systemService");
	private static ZyWorkFlowService zyWorkFlowService = ApplicationContextUtil.getBean("zyWorkFlowService");
	
	private int beginR;
	private int endR;
	private List<ZyInspectionPlanEntity> zyIps;
	private TSUser user;
	private String remarks;
	
	
	public ZyProcessThreadSetpTwo(int beginR,int endR,List<ZyInspectionPlanEntity> zyIps,TSUser user,String remarks){
		this.beginR = beginR;
		this.endR = endR;
		this.zyIps = zyIps;
		this.user = user;
		this.remarks = remarks;
	}
	
	@Override
	public Map<String, Object> call(){
		Map<String, Object> rsMap = new HashMap<String, Object>();
		try {
			for(int i = beginR;i <= endR;i++ ){
				ZyInspectionPlanEntity zyIP = zyIps.get(i);
				if(StringUtil.isNotEmpty(zyIP.getProcesssId())){
					Map<String,Object> returnMap = zyWorkFlowService.processComplete("1",zyIP.getProcesssId(),"","","");
//					TSUser user = ResourceUtil.getSessionUserName ();
					//保存巡检计划流程记录
					ZyPlanRecordEntity zyPlanRecord = new ZyPlanRecordEntity();
					zyPlanRecord.setBusId(zyIP.getId());
					zyPlanRecord.setPlanState(zyIP.getState());
					zyPlanRecord.setProcInsId(zyIP.getProcesssId());
					zyPlanRecord.setRemark("巴可商务主管分配巡检任务，备注："+remarks);
					zyPlanRecord.setTsUser(user);
					zyPlanRecord.setTaskId("");
					zyPlanRecord.setCreateDate(new Date());
					zyPlanRecord.setCreateBy(user.getUserName());
					zyPlanRecord.setCreateName(user.getRealName());
					systemService.save(zyPlanRecord);
				}
			}
		} catch (Exception e) {
			System.err.println("[推动巡检流程-分派到第三方] - 事务出现异常,回滚事务！");
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//此处采用手动回滚
		}
		return rsMap;
	}
	
	
}
